package com.example.feifei.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.LinearLayout;

import com.example.feifei.newproject.R;

/**
 * 记录触摸点，进行动画实现
 * Created by feifei on 2017/7/17.
 *
 */

public class TouchLayoutView extends LinearLayout{

    private View layout_view;

    int x = 0;//记录点击中心距左边距
    int y = 0;//记录点击中心距右边距


    private AnimatorSet animatorSet;

    private View circleView;
    private DotsView vDotsView;
    private InterDotsView view_interdots;
    private Thread animatorThread;


    private static final AccelerateDecelerateInterpolator ACCELERATE_DECELERATE_INTERPOLATOR = new AccelerateDecelerateInterpolator();


    public TouchLayoutView(Context context) {
        super(context);
        initView();

    }

    public TouchLayoutView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public TouchLayoutView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        layout_view = LayoutInflater.from(getContext()).inflate(R.layout.layout_flowerview,this,true);
        circleView = layout_view.findViewById(R.id.progress);
        vDotsView = (DotsView) layout_view.findViewById(R.id.view_dots);
        view_interdots = (InterDotsView) layout_view.findViewById(R.id.view_interdots);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
       int childCount = getChildCount();
        //设置一个变量保存父View的左侧距离
//        int mleft = 100;
//        for(int i =0;i<getChildCount();i++){
//            if(getChildAt(i).getId()==R.id.progress){
//                circleView = getChildAt(i);
//            }
//            if(getChildAt(i).getId() == R.id.view_dots){
//                vDotsView = (DotsView) getChildAt(i);
//            }
//        }
        if(null != circleView ){
            int childHeight = circleView.getMeasuredHeight();//获得子View的高度
            int childWidth = circleView.getMeasuredWidth();//获得子View的宽度
            int height = getHeight()/2 - circleView.getMeasuredHeight()/2;
            circleView.layout(200-childWidth/2,200-childHeight/2,200+childWidth/2,200+childHeight/2);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction())  {
            case MotionEvent.ACTION_DOWN:
               x = (int) event.getX();
               y = (int) event.getY();
                Log.e("tag","横向坐标x==="+x+"纵向坐标y=="+y);
                vDotsView.layout(x-vDotsView.getMeasuredWidth()/2,y-vDotsView.getMeasuredHeight()/2,x+vDotsView.getMeasuredWidth()/2,y+vDotsView.getMeasuredHeight()/2);
                view_interdots.layout(x-vDotsView.getMeasuredWidth()/2,y-vDotsView.getMeasuredHeight()/2,x+vDotsView.getMeasuredWidth()/2,y+vDotsView.getMeasuredHeight()/2);
                startAnimation();
                vDotsView.invalidate();
                break;

        }
        return true;
    }

    private void startAnimation(){


        if(animatorSet==null){
            animatorSet = new AnimatorSet();
        }

        vDotsView.setCurrentProgress(0);

        ObjectAnimator dotsAnimator = ObjectAnimator.ofFloat(vDotsView,DotsView.DOTS_PROGRESS,0,1f);
        dotsAnimator.setStartDelay(0);

        final ObjectAnimator interdotsAnimator = ObjectAnimator.ofFloat(view_interdots,InterDotsView.DOTS_PROGRESS,0,1f);
        interdotsAnimator.setStartDelay(200);

        animatorSet.playTogether(dotsAnimator,interdotsAnimator);
        animatorSet.setDuration(700);
        animatorSet.setInterpolator(ACCELERATE_DECELERATE_INTERPOLATOR);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationCancel(Animator animation) {
                Log.e("tag","监听动画取消");
                vDotsView.setCurrentProgress(0);
                view_interdots.setCurrentProgress(0);
            }
        });
        animatorSet.start();


    }



}
